package org.abettor.pushbox.activity2;

import org.abettor.pushbox.R;
import org.abettor.pushbox.download.DownloadUserInfo;
import org.abettor.pushbox.download.ParaDownloadRetValue;
import org.abettor.pushbox.model.UserInfoBean;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * 查看用户的详情
 * @author jzh
 *
 */
public class ShowUserDetailActivity extends Activity{
	private Button sendMsgButton;//发送消息的按钮
	private ProgressDialog progress;// 进度条对话框
	private DownloadUserInfo download = null;
	private Handler mHandler = new Handler();
	private String nickName;//昵称
	private int userId = -1;//用户的id
	private UserInfoBean userBean;
	// 按钮监听的监听器
	protected class ButtonOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if(v==sendMsgButton){
				Intent in = new Intent();
				Bundle extras = new Bundle();
				extras.putInt("_id", userId);
				in.putExtras(extras);
				in.setClass(ShowUserDetailActivity.this,
						SiteMessageActivity.class);
				startActivity(in);
			}
		}
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		download = new DownloadUserInfo(this,null);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_user_detail_activity);
		sendMsgButton = (Button)this.findViewById(R.id.sendMsg);
		sendMsgButton.setOnClickListener(new ButtonOnClickListener());
	}
	public void onStart(){
		super.onStart();
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		userId = extras.getInt("_id");
		nickName = extras.getString("nick_name");
		readDataFromNet();
	}
	//从网络上读取数据
	private void readDataFromNet(){
		progress = new ProgressDialog(this);
		progress.setTitle(R.string.show_user_detail_activity);
		progress.setMessage(this
				.getText(R.string.show_user_detail_activity_loading));
		progress.show();
		Thread thread = new Thread() {
			@Override
			public void run() {
				String ret = download.readUserStr(nickName);
				progress.dismiss();
				if (ret == null) {
					readMessageError();
					return;
				}
				userBean = ParaDownloadRetValue.paraUserInfo(ret);
				if (userBean == null) {
					readMessageError();
					return;
				}
				// 调用设置界面的接口
				mHandler.post(new Runnable() {
					@Override
					public void run() {
						setViewData();
					}
				});
			}
		};
		thread.start();
	}
	//设置用户的消息
	private void setViewData(){
		TextView showNick = (TextView) this.findViewById(R.id.showNick);
		TextView showRegtime = (TextView) this.findViewById(R.id.showRegtime);
		TextView showMyUpload = (TextView) this.findViewById(R.id.showMyUpload);
		TextView showMyResolved = (TextView) this
				.findViewById(R.id.showMyResolved);
		TextView showMyQuesstion = (TextView) this
				.findViewById(R.id.showMyQuesstion);
		TextView showMyAnswer = (TextView) this.findViewById(R.id.showMyAnswer);
		showNick.setText(nickName);
		showRegtime.setText(userBean.getRegtime());
		showMyUpload.setText(userBean.getMyUploadCount());
		showMyResolved.setText(userBean.getMyResolvedCount());
		showMyQuesstion.setText(userBean.getMyQuesstionCount());
		showMyAnswer.setText(userBean.getMyAnswerCount());
	}
	// 读取网络信息失败,显示的对话框
	private void readMessageError() {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						ShowUserDetailActivity.this);
				final AlertDialog alert = builder.create();
				alert.setTitle(R.string.error);
				alert.setMessage(ShowUserDetailActivity.this
						.getText(R.string.read_net_error));
				alert.setButton(
						DialogInterface.BUTTON_POSITIVE,
						ShowUserDetailActivity.this.getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								readDataFromNet();
							}
						});
				alert.setButton(DialogInterface.BUTTON_NEGATIVE,
						ShowUserDetailActivity.this
								.getText(android.R.string.cancel),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								ShowUserDetailActivity.this.finish();
							}
						});
				alert.show();
			}
		});
	}
}
